//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "AnalyticDiscreteGeometricAveragePriceAsianEngine.h"
using namespace Cephei::QL::Pricingengines::Asian;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
using namespace Cephei::QL::Processes;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Asian::CAnalyticDiscreteGeometricAveragePriceAsianEngine::CAnalyticDiscreteGeometricAveragePriceAsianEngine (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CGeneralizedBlackScholesProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phAnalyticDiscreteGeometricAveragePriceAsianEngine = NULL;
#endif
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetShared ()); 
        _ppAnalyticDiscreteGeometricAveragePriceAsianEngine = new boost::shared_ptr<QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine> (new QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine ( _process ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Pricingengines::Asian::CAnalyticDiscreteGeometricAveragePriceAsianEngine::CAnalyticDiscreteGeometricAveragePriceAsianEngine (boost::shared_ptr<QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAnalyticDiscreteGeometricAveragePriceAsianEngine = NULL;
#endif
	_ppAnalyticDiscreteGeometricAveragePriceAsianEngine = &childNative;
    
}
Cephei::QL::Pricingengines::Asian::CAnalyticDiscreteGeometricAveragePriceAsianEngine::CAnalyticDiscreteGeometricAveragePriceAsianEngine (QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phAnalyticDiscreteGeometricAveragePriceAsianEngine = NULL;
#endif
	_ppAnalyticDiscreteGeometricAveragePriceAsianEngine = new boost::shared_ptr<QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine> (&childNative);
    
    _AnalyticDiscreteGeometricAveragePriceAsianEngineOwner = owner;
    
}

Cephei::QL::Pricingengines::Asian::CAnalyticDiscreteGeometricAveragePriceAsianEngine::CAnalyticDiscreteGeometricAveragePriceAsianEngine (CAnalyticDiscreteGeometricAveragePriceAsianEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAnalyticDiscreteGeometricAveragePriceAsianEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAnalyticDiscreteGeometricAveragePriceAsianEngine = new boost::shared_ptr<QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Asian::CAnalyticDiscreteGeometricAveragePriceAsianEngine::CAnalyticDiscreteGeometricAveragePriceAsianEngine (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAnalyticDiscreteGeometricAveragePriceAsianEngine = NULL;
#endif
	if (!t->IsSubclassOf(CAnalyticDiscreteGeometricAveragePriceAsianEngine::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Asian::CAnalyticDiscreteGeometricAveragePriceAsianEngine::CAnalyticDiscreteGeometricAveragePriceAsianEngine (QuantLib::Handle<QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAnalyticDiscreteGeometricAveragePriceAsianEngine = &childNative;
	_ppAnalyticDiscreteGeometricAveragePriceAsianEngine = &static_cast<boost::shared_ptr<QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine>>(childNative.currentLink());
    
    _AnalyticDiscreteGeometricAveragePriceAsianEngineOwner = owner;
}
Cephei::QL::Pricingengines::Asian::CAnalyticDiscreteGeometricAveragePriceAsianEngine::CAnalyticDiscreteGeometricAveragePriceAsianEngine (QuantLib::Handle<QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAnalyticDiscreteGeometricAveragePriceAsianEngine = &childNative;
	_ppAnalyticDiscreteGeometricAveragePriceAsianEngine = &static_cast<boost::shared_ptr<QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Asian::CAnalyticDiscreteGeometricAveragePriceAsianEngine::CAnalyticDiscreteGeometricAveragePriceAsianEngine (QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAnalyticDiscreteGeometricAveragePriceAsianEngine = NULL;
#endif
	_ppAnalyticDiscreteGeometricAveragePriceAsianEngine = new boost::shared_ptr<QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine> (new QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Asian::CAnalyticDiscreteGeometricAveragePriceAsianEngine::~CAnalyticDiscreteGeometricAveragePriceAsianEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppAnalyticDiscreteGeometricAveragePriceAsianEngine != NULL)
    {
	    delete _ppAnalyticDiscreteGeometricAveragePriceAsianEngine;
        _ppAnalyticDiscreteGeometricAveragePriceAsianEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Asian::CAnalyticDiscreteGeometricAveragePriceAsianEngine::!CAnalyticDiscreteGeometricAveragePriceAsianEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppAnalyticDiscreteGeometricAveragePriceAsianEngine != NULL)
    {
	    delete _ppAnalyticDiscreteGeometricAveragePriceAsianEngine;
    }
}
QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine& Cephei::QL::Pricingengines::Asian::CAnalyticDiscreteGeometricAveragePriceAsianEngine::GetReference ()
{
    if (_ppAnalyticDiscreteGeometricAveragePriceAsianEngine == NULL) throw gcnew NativeNullException ();
	return **_ppAnalyticDiscreteGeometricAveragePriceAsianEngine;
}
boost::shared_ptr<QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine>& Cephei::QL::Pricingengines::Asian::CAnalyticDiscreteGeometricAveragePriceAsianEngine::GetShared ()
{
    if (_ppAnalyticDiscreteGeometricAveragePriceAsianEngine == NULL) throw gcnew NativeNullException ();
	return *_ppAnalyticDiscreteGeometricAveragePriceAsianEngine;
}
QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine* Cephei::QL::Pricingengines::Asian::CAnalyticDiscreteGeometricAveragePriceAsianEngine::GetPointer ()
{
    if (_ppAnalyticDiscreteGeometricAveragePriceAsianEngine == NULL) throw gcnew NativeNullException ();
	return &**_ppAnalyticDiscreteGeometricAveragePriceAsianEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine>& Cephei::QL::Pricingengines::Asian::CAnalyticDiscreteGeometricAveragePriceAsianEngine::GetHandle ()
{
	if (_phAnalyticDiscreteGeometricAveragePriceAsianEngine == NULL)
	{
		_phAnalyticDiscreteGeometricAveragePriceAsianEngine = new Handle<QuantLib::AnalyticDiscreteGeometricAveragePriceAsianEngine> (*_ppAnalyticDiscreteGeometricAveragePriceAsianEngine);
	}
	return *_phAnalyticDiscreteGeometricAveragePriceAsianEngine;
}
#endif
bool Cephei::QL::Pricingengines::Asian::CAnalyticDiscreteGeometricAveragePriceAsianEngine::HasNative () 
{
	return (_ppAnalyticDiscreteGeometricAveragePriceAsianEngine != NULL);
}

Cephei::QL::Pricingengines::Asian::IAnalyticDiscreteGeometricAveragePriceAsianEngine^ Cephei::QL::Pricingengines::Asian::CAnalyticDiscreteGeometricAveragePriceAsianEngine::Calculate::get ()
{
    try
    {
    	(*_ppAnalyticDiscreteGeometricAveragePriceAsianEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Asian::IAnalyticDiscreteGeometricAveragePriceAsianEngine^ Cephei::QL::Pricingengines::Asian::CAnalyticDiscreteGeometricAveragePriceAsianEngine_Factory::Create (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process)
{
    return gcnew CAnalyticDiscreteGeometricAveragePriceAsianEngine ( process);
}
